<template>
  <div class="login-container dis-flex flex-x-center">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <img
          src="http://file.vekise.com/images/20210319/20210319095945bebd31653.png?v=1.1.39"
          width="50"
        >
        <p class="title">凝一服装信息化集成系统</p>
      </div>
      <div class="login-form-item">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="账号" name="username" type="text" />
        </el-form-item>
      </div>
      <div class="login-form-item">
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </div>
      <div class="login-form-item dis-flex flex-x-between">
        <el-form-item prop="verifycode">
          <span class="svg-verification">
            <svg-icon icon-class="verification" />
          </span>
          <el-input ref="verifycode" v-model="loginForm.verifycode" placeholder="验证码" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>
        <img :src="previewImage" height="40" @click="getCaptcga">
      </div>
      <el-button :loading="loading" type="primary" class="login" @click="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { captcha } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        verifykey: ''
      },
      loginRules: {
        username: { required: true, trigger: 'blur', message: '请输入账号' },
        password: { required: true, trigger: 'blur', message: '请输入密码' },
        verifycode: { required: true, trigger: 'blur', message: '请输入验证码' }
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      previewImage: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCaptcga()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 获取验证码
    getCaptcga() {
      this.loginForm.verifykey = Math.round(new Date() / 1000)
      captcha({ verifykey: this.loginForm.verifykey }).then(res => {
        return 'data:image/png;base64,' + btoa(
          new Uint8Array(res)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        )
      }).then(data => {
        this.previewImage = data // 图片地址 <img src='data' />
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((res) => {
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch((res) => {
              this.loading = false
              this.loginForm.verifycode = ''
              this.getCaptcga()
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    background: white;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: white !important;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      height: 40px;
    }
  }
  .el-form-item__content{
    height: 40px;
  }

  .el-form-item {
    border: 1px solid #D9D9D9;
    color: #454545;
    padding-left: 15px;
  }
  .el-form-item-text {
    text-align: left;
    margin: 30px 60px 10px 60px;
  }
  .login {
    color: #fff;
    border-radius: 0;
    width: 100%;
    border: 1px solid transparent;
    cursor: pointer;
    outline: none;
    padding: 10px 18px;
    font-size: 18px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(http://file.vekise.com/images/20210319/20210319095302c4fe58835.jpg)
    center no-repeat;
  background-size: 100%;
  overflow: hidden;

  .login-form {
    display: inline-block;
    vertical-align: middle;
    position: relative;
    width: 480px;
    height: 520px;
    margin: auto;
    overflow: hidden;
    background-color: white;
    border-radius: 20px;
    padding: 0 60px;
    opacity: 0.96;
    .login-form-item{
      margin-bottom: 30px;
    }
  }

  .tips {
    font-size: 14px;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    vertical-align: middle;
    display: inline-block;
  }
  .svg-verification{
    vertical-align: middle;
    display: inline-block;
    font-size: 20px;
  }

  .title-container {
    position: relative;
    padding: 35px 0 0 0;
    text-align: center;

    .title {
      font-size: 16px;
      margin-bottom: 60px;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    // color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
